<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\Services;

import("Classes.Services.AbstractService");
import("Classes.Util.HTTP.HTTPRequest");

use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Factory\ObjectFactory;
use \PsiFramework\Classes\Services\AbstractService;
use \PsiFramework\Classes\Util\HTTP\HTTPRequest;
use \PsiFramework\Classes\Util\HTTP\Request;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Service de listing des auteurs
 *
 * @package Services
 */
class CreateSiteCategoriesService extends AbstractService
{

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * The main function of the method which is called every time
     * an service is created by the front controller.
     *
     * @param $request      the HTTP request to be treated by the action
     */
    public function execute(Request $request)
    {
        parent::execute($request);

        $siteId = $request->getParameter("site_id");
        $siteUri = $request->getParameter("site_uri");

        $siteDao = DAOFactory::getDAO('AlloMediaSite');

        $site = null ;
        if (!isNullOrEmptyString($siteId)) {
            $site = $siteDao->retrieveByPk($siteId);
        } else if (!isNullOrEmptyString($siteUri)) {
            $site = $siteDao->retrieveByUri($siteUri);
        }

        if (!is_null($site)) {
            $httpRequest
                = new HTTPRequest(
                    $site->getUri().'/webservices/categories_list.php'
                );
            $response = $httpRequest->execute(HTTPRequest::$get);

            $document = new \DomDocument();
            $document->loadXML($response);
            $allomediaElement = $document->documentElement;

            $sites = $allomediaElement->getElementsByTagName("site");
            if (!is_null($sites)) {
                $siteEl = $sites->item(0);

                $categoryDao = DAOFactory::getDAO('SiteCategory');
                $categoryLinkDao = DAOFactory::getDAO('CategoryLink');

                $categories = $siteEl->getElementsByTagName("category");
                foreach ($categories as $categoryEl) {
                    $id = $categoryEl->getAttribute("id");
                    $name = $categoryEl->getAttribute("name");

                    $category
                        = $categoryDao->retrieveByPartialName($name, true);

                    if (is_null($category)) {
                        $category = ObjectFactory::getNewObject("SiteCategory");
                        $category->setName($name);
                    }

                    $style = $site->getStyle();
                    $curStyle = $category->getStyle();
                    if ((is_null($curStyle) || (!is_null($curStyle) && $curStyle->getName() == 'Default'))
                        && !is_null($style)
                    ) {
                        $category->setStyle($style);
                    }

                    $profile = $site->getProfile();
                    $curProfile = $category->getProfile();
                    if ((is_null($curProfile) || (!is_null($curProfile) && $curProfile->getName() == 'Default'))
                        && !is_null($profile)
                    ) {
                        $category->setProfile($profile);
                    }

                    $categoryDao->save($category);

                    $categoryLink = ObjectFactory::getNewObject("CategoryLink");
                    $categoryLink->setSite($site);
                    $categoryLink->setCategory($category);
                    $categoryLink->setIdForSite($id);

                    $categoryLinkDao->save($categoryLink);
                }

                echo "ok" ;
            }
        }

    }
}
